package com.itfreer.file.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.itfreer.file.service.IObjectManageService;

/**
 * 定义对象管理接口
 */
public class OssObjectManage implements IObjectManageService, BeanFactoryAware {

	protected static BeanFactory beanFactory;

	@Override
	public void setBeanFactory(BeanFactory bf) throws BeansException {
		beanFactory = bf;
	}
	
	private AliyunOssPara aliyunOssPara;
	
	private AliyunOssPara getPara(){
		if(aliyunOssPara==null){
			aliyunOssPara = (AliyunOssPara) beanFactory.getBean("aliyunOssPara");
		}
		return aliyunOssPara;
	}
	
	private OSSClient getClient(){
		OSSClient ossClient = new OSSClient(getPara().getEndpoint(), getPara().getAccessKeyId(), getPara().getAccessKeySecret());
		return ossClient;
	}
	
	/**
	 * 对象是否存在
	 * @param bucketName	存储块名称
	 * @param objectKey		对象名称主键
	 */
	public boolean doesObjectExist(String bucketName, String objectKey){
		// 创建OSSClient实例
		OSSClient ossClient = getClient();
		// Object是否存在
		boolean found = ossClient.doesObjectExist(bucketName, objectKey);
		// 关闭client
		ossClient.shutdown();
		return found;
	}
	
	/**
	 * 获取对象名称
	 * @param bucketName	存储块名称
	 * @param prefix		限定返回的object key必须以prefix作为前缀
	 * @param marker		设定结果从marker之后按字母排序的第一个开始返回
	 * @param maxKeys		限定此次返回object的最大数，如果不设定，默认为100，max-keys取值不能大于1000
	 * @return
	 */
	public List<Map<String, Object>> listObjects(String bucketName, String prefix, String marker, Integer maxKeys){
		OSSClient ossClient = getClient();
		ListObjectsRequest request = new ListObjectsRequest(bucketName);
		if(prefix!=null){
			request.withPrefix(prefix);
		}
		
		if(maxKeys!=null){
			request.withMaxKeys(maxKeys);
		}
		
		if(marker!=null){
			request.withMarker(marker);
		}
		
		// 列举Object
		ObjectListing objectListing = ossClient.listObjects(request);
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		
		List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
		for (OSSObjectSummary s : sums) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("key", s.getKey());
			item.put("size", String.valueOf(s.getSize()));
			item.put("lastModified", s.getLastModified().toString());
			item.put("owner", s.getOwner().toString());
			
			mapList.add(item);
		}
		// 关闭client
		ossClient.shutdown();
		return mapList;
	}
	
	/**
	 * 删除对象
	 * @param bucketName	存储块名称
	 * @param objectKey		对象名称主键
	 */
	public void deleteObject(String bucketName, String objectKey){
		OSSClient ossClient = getClient();
		// 删除Object
		ossClient.deleteObject(bucketName, objectKey);
		// 关闭client
		ossClient.shutdown();
	}
	
	/**
	 * 删除多个对象
	 * @param bucketName	存储块名称
	 * @param keys			对象名称主键
	 * @return
	 */
	public void deleteObjects(String bucketName, List<String> keys){
		OSSClient ossClient = getClient();
		DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
		deleteObjectsResult.getDeletedObjects();
		// 关闭client
		ossClient.shutdown();
	}
	
	/**
	 * 复制对象
	 * @param sourceBucketName			源存储块名称
	 * @param sourceKey					源对象名称主键
	 * @param destinationBucketName		目标储块名称
	 * @param destinationKey			目标对象名称主键
	 * @return
	 */
	public void copyObject(String sourceBucketName, String sourceKey, String destinationBucketName, String destinationKey){
		OSSClient ossClient = getClient();
		// 拷贝Object
		ossClient.copyObject(sourceBucketName, sourceKey, destinationBucketName, destinationKey);
		// 关闭client
		ossClient.shutdown();
	}
}
